package com.example.jh352160.surfingtv.Index.customView;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.example.jh352160.surfingtv.Index.MainActivity;
import com.example.jh352160.surfingtv.Index.customView.adapter.ThematicRecyclerViewAdapter;
import com.example.jh352160.surfingtv.R;
import com.example.jh352160.surfingtv.network.item.IndexItem;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jh352160 on 2016/7/20
 */

public class IndexThematic extends LinearLayout {

    Context context;
    List<ImageView> imageViews;
    List<String> urls;
    ThematicRecyclerViewAdapter adapter;
    List<Integer> contentIds;

    public IndexThematic(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public IndexThematic(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    private void init() {
        LayoutInflater.from(context).inflate(R.layout.index_thematic, this);

        //api
        imageViews = new ArrayList<>();
        urls = new ArrayList<>();
        contentIds = new ArrayList<>();
        IndexItem.DataBean data = MainActivity.getData();
        imageViews.clear();
        for (IndexItem.DataBean.TodaySubjectBean i : data.getTodaySubject()) {
            String url = i.getImageUrl();
            ImageView imageView = new ImageView(context);
            int contentId = i.getContentId();
            Glide.with(context).load(url).into(imageView);
            imageViews.add(imageView);
            urls.add(url);
            contentIds.add(contentId);
        }

//        int[] imageId={R.drawable.thematic_image1,R.drawable.thematic_image2,R.drawable.thematic_image3,R.drawable.thematic_image4};
//        for(int i:imageId){
//            ImageView image=new ImageView(context);
////            image.setImageResource(i);
//            image.setBackgroundColor(getResources().getColor(android.R.color.white));
//            imageViews.add(image);
//        }
        adapter = new ThematicRecyclerViewAdapter(context, imageViews, urls, contentIds);
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        // TODO: 2016/7/20 search why need use LinearLayoutManager
        LinearLayoutManager llm = new LinearLayoutManager(context);
        llm.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(llm);
        recyclerView.setAdapter(adapter);
    }

}
